package com.lzj.demo.controller;


import cn.hutool.json.JSONUtil;
import com.lzj.demo.common.ResponseObject;
import com.lzj.demo.dto.Order;
import com.lzj.demo.vo.CreateOrderVo;
import com.lzj.demo.dto.TbOrder;
import com.lzj.demo.service.TbOrderService;
import com.lzj.demo.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private TbOrderService tbOrderService;

    /**
     * 分布式事务演示
     * */
    @GetMapping("/create")
    public ResponseObject create(Order order) {
        return orderService.createOrder(order);
    }

    /**
     * 领域驱动设计思想(DDD)demo演示
     * */
    @PostMapping("/add")
    public ResponseObject add(CreateOrderVo createOrderVo) {
        //模拟用户下订单
        TbOrder tbOrder = tbOrderService.createOrder(createOrderVo);
        log.info("用户下单成功,订单信息:{}", JSONUtil.toJsonStr(tbOrder));

        //模拟生单时设置收货地址，然后用户修改地址
        String newAddress = "修改地址";
        tbOrderService.modifyAddress(tbOrder.getId(),newAddress);
        log.info("用户修改收货地址成功,订单信息:{}", JSONUtil.toJsonStr(tbOrder));

        //模拟用户支付
        tbOrderService.paySuccess(tbOrder.getId());
        log.info("用户支付成功,订单信息:{}", JSONUtil.toJsonStr(tbOrder));
        return ResponseObject.ok();
    }
}
